package cn.jsony.org.service.impl;

import cn.jsony.basic.config.BusinessException;
import cn.jsony.basic.service.impl.BaseServiceImpl;
import cn.jsony.basic.util.BaiduAiUtils;
import cn.jsony.basic.util.Md5Utils;
import cn.jsony.basic.util.StrUtils;
import cn.jsony.org.domain.*;
import cn.jsony.org.mapper.EmployeeMapper;
import cn.jsony.org.mapper.ShopAuditLogMapper;
import cn.jsony.org.mapper.ShopMapper;
import cn.jsony.org.service.IShopService;
import cn.jsony.user.domain.Logininfo;
import cn.jsony.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;


/**
 * 业务实现类：商铺
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper eMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

/* 商家入驻逻辑步骤：
 * 一：检验
 * 1.1.每个参数的空值判断
 * 1.2.两次密码不一致校验
 * 1.3.该店铺是否已经被入驻过
 * 二：入驻业务
 * 2.1.保存店铺管理员信息t_employee
 * 2.2.保存店铺信息t_shop
 * 2.2.将店铺的id更新到t_employee中*/


    @Override
    public void settlement(Shop shop) throws MessagingException {
        if(StringUtils.isEmpty(shop.getName())){//验证店铺名不能为空
            throw new BusinessException("店铺名不能为空");
        }

        //2.判断是否入驻过
        Shop shop1 = shopMapper.loadByNameAndAddress(shop);
        if(shop1!=null){
            throw new BusinessException("该店铺已经入驻了，忘记密码请联系管理员");
        }

        //3.文本审核
        if (!BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("名字不合规");
        }
        System.out.println(shop.getLogo());
        //3.1图片审核
        String imgUrl = "http://123.207.27.208"+shop.getLogo();
        if (!BaiduAiUtils.imgCensor(imgUrl)){
            throw new BusinessException("图片不合规");
        }


        //2.入驻业务
        //  2.1.保存店铺管理员信息t_employee
        Employee admin = shop.getAdmin();

        //2.1.1处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword()+salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        eMapper.save(admin);

        //  2.2.保存店铺信息t_shop,把店铺管理员ID-admin_id保存到t_shop
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        //2.4.保存登录信息t_logininfo
        Logininfo logininfo = employee2Logininfo(admin);
        logininfoMapper.save(logininfo);

        //  2.5.将店铺的id和logininfo_id更新到t_employee中
        admin.setShop_id(shop.getId());
        admin.setLogininfo_id(logininfo.getId());
        eMapper.update(admin);

    }

    private Logininfo employee2Logininfo(Employee admin) {
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(admin,logininfo);

        logininfo.setType(0);
        return logininfo;
    }

    /*审核通过*/
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {

        //1.改变店铺的状态1->2
        //通过ID找到当前日志对应的shop对象
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.getAudit_id(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //发送复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        helper.setFrom("1104829900@qq.com");

        helper.setSubject("审核通过的激活文件");

        helper.setText("<h1>你的店铺已经注册</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' >" +
                "<a href='http://localhost:8080/shop/active/" +
                shop.getId()+"'>点击该链接激活</a>");

        //添加附件
        helper.addAttachment("b1.jpg",new File("D:\\java学习\\01_学习资料\\03_images\\b1.jpg"));


        Employee employee = eMapper.loadById(shop.getAdmin_id());

        String email = employee.getEmail();

        helper.setTo(email);

        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改变店铺的状态1->4
        //通过ID找到当前日志对应的shop对象
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.getAudit_id(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //发送复杂邮件:创建复杂的邮件对象
        MimeMessage message = javaMailSender.createMimeMessage();

        /*(MimeMessage mimeMessage, boolean multipart, @Nullable String encoding)*/
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        //发件人
        helper.setFrom("1104829900@qq.com");

        //收件人
        Employee employee = eMapper.loadById(shop.getAdmin_id());
        helper.setTo(employee.getEmail());

        //主题
        helper.setSubject("店铺审核失败");

        //内容
        helper.setText("<h2>你的店铺审核失败，请重新入驻</br>" +
                "<a href='http://localhost:8081/#/register'> 点击链接重新入驻</a></2>",true);

        javaMailSender.send(message);
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    @Override
    public void emailPass(Long id) {
        Shop shopById = shopMapper.loadById(id);
        shopById.setState(3);
        shopMapper.update(shopById);
    }
}
